﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Meridian.Views
{
    /// <summary>
    /// Interaction logic for SettingsView.xaml
    /// </summary>
    public partial class SettingsView : UserControl
    {
        public event EventHandler Closed;
        private bool restartRequired;
        private readonly List<string> langCodes = new List<string>();

        public SettingsView()
        {
            InitializeComponent();
        }

        private void UserControlLoaded(object sender, RoutedEventArgs e)
        {
            var fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
            VersionString.Text = Assembly.GetExecutingAssembly().GetName().Version + ".beta2." + fileInfo.LastWriteTimeUtc.ToString("yyMMdd-HHmm");

            FirstLoadCountSlider.Value = App.Settings.FirstLoadItemsCount;
            DynamicLoadCountSlider.Value = App.Settings.DynamicLoadItemsCount;
            AccentColorBox.Text = App.Settings.AccentColor;

            LanguageBox.Items.Add(new ComboBoxItem() { Content = CultureInfo.GetCultureInfo("en").NativeName });
            langCodes.Add("en");
            LanguageBox.Items.Add(new ComboBoxItem() { Content = CultureInfo.GetCultureInfo("ru").NativeName });
            langCodes.Add("ru");

            LanguageBox.Text = CultureInfo.GetCultureInfo(App.Settings.Language).NativeName;
            AutoUpdateCheckBox.IsChecked = App.Settings.CheckUpdates;
        }

        private void FirstLoadCountSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((int)FirstLoadCountSlider.Value > 0)
                FirstLoadCountTextBlock.Text = string.Format(Properties.Resources.FirstLoadCount, (int)FirstLoadCountSlider.Value);
            else
                FirstLoadCountTextBlock.Text = string.Format(Properties.Resources.FirstLoadCount, Properties.Resources.LoadMax);
        }

        private void DynamicLoadCountSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if ((int)DynamicLoadCountSlider.Value > 0)
                DynamicLoadCountTextBlock.Text = string.Format(Properties.Resources.DynamicLoadCount, (int)DynamicLoadCountSlider.Value);
            else
                DynamicLoadCountTextBlock.Text = string.Format(Properties.Resources.DynamicLoadCount, Properties.Resources.LoadMax);
        }

        void ApplySettings()
        {
            App.Settings.FirstLoadItemsCount = (int)FirstLoadCountSlider.Value;
            App.Settings.DynamicLoadItemsCount = (int)DynamicLoadCountSlider.Value;
            App.Settings.CheckUpdates = AutoUpdateCheckBox.IsChecked == true;

            if (App.Settings.AccentColor != AccentColorBox.Text)
            {
                restartRequired = true;
                App.Settings.AccentColor = AccentColorBox.Text;
            }

            if (App.Settings.Language != CultureInfo.GetCultureInfo(App.Settings.Language).NativeName)
            {
                restartRequired = true;
                App.Settings.Language = langCodes[LanguageBox.SelectedIndex];
            }

            App.Settings.Save(App.Root + "\\Meridian.config");
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void Close()
        {
            if (restartRequired)
            {
                Process.Start(Application.ResourceAssembly.Location);
                Application.Current.Shutdown();
                return;
            }

            if (Closed != null)
                Closed(this, EventArgs.Empty);
        }

        private void SaveButtonClick(object sender, RoutedEventArgs e)
        {
            ApplySettings();
            Close();
        }

        private void AccentColorBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBoxItem)e.AddedItems[0]).Content.ToString() != App.Settings.AccentColor)
                RestartRequiredText.Visibility = Visibility.Visible;
        }

        private void LanguageComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBoxItem)e.AddedItems[0]).Content.ToString() != CultureInfo.GetCultureInfo(App.Settings.Language).NativeName)
                RestartRequiredText.Visibility = Visibility.Visible;
        }

        private void CheckButtonClick(object sender, RoutedEventArgs e)
        {
            App.ViewModel.CheckForUpdates(true);
            CheckButton.IsEnabled = false;
        }
    }
}
